package Server;
// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 24/05/2011 14:48:34
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Server.java

import java.io.PrintStream;
import java.sql.*;
import ocsf.server.AbstractServer;
import ocsf.server.ConnectionToClient;


public class Server extends AbstractServer
{

    public Server(int port)
    {
        super(port);
    }

    public void handleMessageFromClient(Object msg, ConnectionToClient client)
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
        catch(Exception exception) { }
        try
        {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/Flower shop", "root", "Braude");
            System.out.println("SQL connection succeed");
          

           if (msg instanceof String[])
           {  
        	   String array[] = (String[])msg;  
        	   
        	   if (array[0].equals("login"))
        	   {
        		   int logstate=login.chackvalidation((com.mysql.jdbc.Connection) conn, array[1], array[2]);
        		   sendToAllClients(logstate);
        	   }
        	   
        	   
        	   
           }
            
            
            
            
            
            
           /* 
            if(msg instanceof String)
                try
                {
                    Statement stmt = conn.createStatement();
                    PreparedStatement ps = conn.prepareStatement("SELECT * FROM clients WHERE Name= ?;");
                    ps.setObject(1, msg);
                    ResultSet rs;
                    for(rs = ps.executeQuery(); rs.next(); sendToAllClients((new StringBuilder("password:")).append(rs.getString(5)).toString()))
                    {
                        sendToAllClients((new StringBuilder("Name:")).append(rs.getString(2)).toString());
                        sendToAllClients((new StringBuilder("Address:")).append(rs.getString(3)).toString());
                        sendToAllClients((new StringBuilder("Username:")).append(rs.getString(4)).toString());
                    }

                    rs.close();
                }
                catch(SQLException e)
                {
                    e.printStackTrace();
                }
            else
                try
                {
                    Statement stmt = conn.createStatement();
                    String array[] = (String[])msg;
                    if(array[1].equals("1"))
                        array[1] = "Address";
                    if(array[1].equals("2"))
                        array[1] = "Username";
                    if(array[1].equals("3"))
                        array[1] = "password";
                    PreparedStatement ps = conn.prepareStatement((new StringBuilder("UPDATE clients SET ")).append(array[1]).append("=? WHERE Name=?;").toString());
                    ps.setObject(1, array[2]);
                    ps.setObject(2, array[0]);
                    ps.executeUpdate();
                    sendToAllClients("Success");
                   
                }
                
                catch(SQLException e)
                {
                    e.printStackTrace();
                }
                */
        }        
        
        catch(SQLException ex)
        {
            System.out.println((new StringBuilder("SQLException: ")).append(ex.getMessage()).toString());
            System.out.println((new StringBuilder("SQLState: ")).append(ex.getSQLState()).toString());
            System.out.println((new StringBuilder("VendorError: ")).append(ex.getErrorCode()).toString());
        }
    }

    protected void serverStarted()
    {
        System.out.println((new StringBuilder("Server listening for connections on port ")).append(getPort()).toString());
    }

    protected void serverStopped()
    {
        System.out.println("Server has stopped listening for connections.");
    }

    public static void main(String args[])
    {
        int port = 0;
        try
        {
            port = Integer.parseInt(args[0]);
        }
        catch(Throwable t)
        {
            port = 5556;
        }
        Server sv = new Server(port);
        try
        {
            sv.listen();
        }
        catch(Exception ex)
        {
            System.out.println("ERROR - Could not listen for clients!");
        }
    }

    public static final int DEFAULT_PORT = 5556;
}
